package com.study.demo.algorithm.first;


public class InsertSort {
	public static void insertSort(int arr[]) {
		for(int i = 1; i<arr.length; i++){
			if (arr[i] < arr[i-1]) {
				int temp = arr[i];
				int j;
				for(j = i -1; j>=0 && temp<arr[j]; j--){
					arr[j+1] = arr[j];
				}
				arr[j+1] =temp; 
			}
		}
	}
	
	public static void main(String[] args) {
		 int array[] = {4,2,1,5};
	        
	        System.out.println("排序之前：");
	        for(int element : array){
	            System.out.print(element+" ");
	        }
	        
	        insertSort(array);
	        System.out.println("\n排序之后：");        
	        for(int element : array){
	            System.out.print(element+" ");
	        }
	}
}
